import java.util.*;
import java.math.BigInteger;
import java.security.*;

public class Generator{
  private static String genSalt(UUID base) {
    String salt = null;
    try {
      MessageDigest md5 = MessageDigest.getInstance("MD5");
      md5.update((base.toString()).getBytes());
      BigInteger hash = new BigInteger(1, md5.digest());
      salt = hash.toString(9);
    } catch (NoSuchAlgorithmException nsae) {
    }
    return salt;
  }


  public static void main(String [] args){
    boolean [][][] usersPer = new boolean[100][100][3];
    String [] surveystartdate = new String[100];
    int [][] usertosurvey = new int[100][100];
      
    String [] bool = {"'y'","'n'"};
    String [] date = {"'2009-12-06'","'2008-06-24'"};
    List<Integer> usersWithCreateRights = new ArrayList<Integer>();

    Random randgen = new Random();

    //Users
    for(int i=2; i<101; i++){
      String salt = Generator.genSalt(UUID.randomUUID());
      String pass = "123wibble" + salt;
      String right = (new Integer(randgen.nextInt(3)+1)).toString();
      System.out.println("INSERT INTO USERS VALUES("+ i + ", 'ala" + i + "', SHA1('" + pass + "')" + ", 1234567890" + ", 'ala" + i + "@pollster.pl', "+ (new Integer(randgen.nextInt(3)+1)).toString() +", "+ right + ", " + bool[randgen.nextInt(2)] +", " + date[randgen.nextInt(2)] +", '" + salt + "');");

      if(!right.equals("1"))
        usersWithCreateRights.add(i);
    }

    String [] date2 = {"'2009-03-02', '2009-03-12','2009-03-22'", "'2009-03-02', '2009-05-02', '2009-06-02'", "'2009-05-02', '2009-06-02','2009-07-02'"};

    //Surveys
    for(int i=1; i<100; i++){
      int d = randgen.nextInt(3);

      System.out.println("INSERT INTO SURVEYS VALUES(" + i +",'Survey "+ i + "', 'Desc for survey " + i + "'," + date2[d] + ", " + bool[randgen.nextInt(2)] + ", " + bool[randgen.nextInt(2)] + ", "  + usersWithCreateRights.get(randgen.nextInt(usersWithCreateRights.size())) + ");");

      switch(d){
        case 0: surveystartdate[i] = "'2009-03-15'"; break;
        case 1: surveystartdate[i] = "'2009-05-05'"; break;
        case 2: surveystartdate[i] = "'2009-06-13'";
      }
    }

    //SurveyPermsions
    int jj=1;
    for(int i=1; i<3000; i++){
      Integer user = new Integer(randgen.nextInt(100)+1);
      Integer survey = new Integer(randgen.nextInt(100)+1);
      Integer per = new Integer(randgen.nextInt(3)+1);
      
      if(!usersPer[user-1][survey-1][per-1]){
        System.out.println("INSERT INTO USERS_SURVEYS_PERMISSIONS VALUES("+ i + ", " + user.toString() + ", " + survey.toString() + ", " + per.toString() + ");");

        usersPer[user-1][survey-1][per-1] = true;

        if(per==3 && i%3==0){
          System.out.println("INSERT INTO FILLED_SURVEYS VALUES("+ jj + ", " + surveystartdate[survey-1] + ", " + user.toString() + ", " + survey.toString() + ", " + bool[randgen.nextInt(2)] +");");

          usertosurvey[user-1][survey-1]=jj;

          jj++;
        }
      }
    }


    //Questions and Answers
    int j=1;
    int k=1;
    int oq=1;
    int cq=1;
    for(int i=1; i<500; i++){
      String type = (new Integer(randgen.nextInt(2)+1)).toString();

      Integer survey = new Integer(randgen.nextInt(99)+1);

      System.out.println("INSERT INTO QUESTIONS VALUES(" + i +","+ survey.toString() + ", " + type + ", 'Jak leci ?');");



      if(type.equals("2")){

          int tmp = randgen.nextInt(2) + 3;

          System.out.println("INSERT INTO CLOSED_QUESTIONS VALUES(" + j +","+ i + ", 1," + (new Integer(randgen.nextInt(tmp)+1)).toString() + ");");

          for(int z =1; z <=  tmp; z++){
            System.out.println("INSERT INTO CQ_POSSIBLE_ANSWERS VALUES(" + k + ", 'odp" + z +"', " + j + ");");
            k++;
          }

          for(jj=0; jj<25; jj++){
            Integer user = new Integer(randgen.nextInt(99)+1);
            if(usertosurvey[user-1][survey-1]!=0){
              System.out.println("INSERT INTO CQ_ANSWERS VALUES(" + cq + ","+ usertosurvey[user-1][survey-1] +", " + (new Integer(randgen.nextInt(tmp) + 1)).toString() + ");");
              cq++;
            }
          }

          j++;
      }
      else{
        for(jj=0; jj<25; jj++){
          Integer user = new Integer(randgen.nextInt(99)+1);
          if(usertosurvey[user-1][survey-1]!=0){
            System.out.println("INSERT INTO OQ_ANSWERS VALUES(" + oq + ","+ usertosurvey[user-1][survey-1] +", 'My answer'," + i + ");");
            oq++;
          }
        }
      }

    }
    
  }
} 
